<?php
namespace Modules\Admin\Model\Data;

use Modules\Admin\Model\Dao\Db\User\Groups as DaoDbUserGroups;
use Modules\Admin\Model\Dao\Db\User\User as DaoDbUserUser;
use Modules\Admin\Model\Dao\Db\User\Group as DaoDbUserGroup;

/**
 * 用户管理
 */
class User {

    /**
     * 获取所有用户信息
     * @return array
     */
    public function getUserList(){
        $user_list = (new DaoDbUserUser())->getList();
        return $user_list;
    }

    /**
     * 根据用户名取用户信息
     * @param $name
     * @return array
     */
    public function getUserInfoByName($name) {
        $info = (new DaoDbUserUser())->getInfoByName($name);
        return $info;
    }

    /**
     * 通过id获取一条用户信息
     * @param integer $uid 用户uid
     * @return array
     */
    public function getUserInfoById($uid){
        $info = (new DaoDbUserUser())->getInfoByUid($uid);
        return $info;
    }

    /**
     * 通过组id获取组成员
     * @param integer $gid 组gid
     * @return array
     */
    public function getUserByGid($gid) {
        $uid = (new DaoDbUserGroup())->getUidByGid($gid);
        if ($uid) {
            return (new DaoDbUserUser())->getListByUid($uid);
        }
        return array();
    }

    /**
     * 获取所有组信息
     * @return array
     */
    public function getGroupList(){
        $group_list = (new DaoDbUserGroups())->getList();
        return $group_list;
    }

    /**
     * 通过id获取一条组信息
     * @param integer $gid 组gid
     * @return array
     */
    public function getGroupOneById($gid){
        $group_info = (new DaoDbUserGroups())->getInfoById($gid);
        return $group_info;
    }

    /**
     * 根据$uid取用户所属组
     * @param $uid
     * @return array
     */
    public function getGroupByUid($uid){
        $group_list = (new DaoDbUserGroup())->getGidByUid($uid);
        return $group_list;
    }

    /**
     * 新增用户组关系
     * @param integer $gid 组id
     * @param integer $uid 用户id
     * @return bool
     */
    public function addUserGroupRel($gid, $uid){
        $data_user_group = new DaoDbUserGroup();
        $ret = $data_user_group->add($gid,$uid);
        return $ret;
    }

    /**
     * 通过组id删除组及相关信息
     * @param integer $gid
     * @return boolean
     */
    public function delGroupOneById($gid) {
        $ret1 = (new DaoDbUserGroups())->delByGid($gid);
        $ret2 = (new DaoDbUserGroup())->delByGid($gid);
        return $ret1 && $ret2;
    }

    /**
     * 删除用户组关系
     * @param integer $gid 组id
     * @param integer $uid 用户id
     * @return bool
     */
    public function delUserGroupRel($gid, $uid) {
        return (new DaoDbUserGroup())->delByGidUid($gid, $uid);
    }

    /**
     * 通过用户id删除所有组关系
     * @param integer $id
     * @return boolean
     */
    public function delGroupRelById($id){
        return (new DaoDbUserGroup())->delById($id);
    }

    /**
     * 校验用户名是否存在
     * @param string $name 用户名
     * @return bool
     */
    public function isExistsByUserName($name){
        $ret = (new DaoDbUserUser())->isExistsByName($name);
        return $ret;
    }

    /**
     * 校验用户组名是否存在
     * @param string $name 用户名
     * @return bool
     */
    public function isExistsByGroupName($name){
        $ret = (new DaoDbUserGroups())->isExistsByName($name);
        return $ret;
    }

    /**
     * 保存组信息
     * @param null $id 组id
     * @param array   $info 组信息
     * @return boolean
     */
    public function saveGroupInfo($info, $id = null) {
        $ret = (new DaoDbUserGroups())->save($info,$id);
        return $ret;
    }

    /**
     * 保存用户信息
     * @param      $info
     * @param null $uid
     * @return bool
     */
    public function saveUserInfo($info, $uid = null) {
        $ret = (new DaoDbUserUser())->save($info, $uid);
        return $ret;
    }

    /**
     * 保存|添加用户信息
     * @param $uid
     * @param $phone
     * @param $nick
     * @param $isadmin
     * @param $status
     * @param $uname
     * @param $description
     * @param $verify
     * @param $password
     * @return bool
     * @throws \Base\Exception\Service
     */
    public function setUserInfo($uid, $phone, $nick, $isadmin, $status, $uname, $description, $verify, $password) {
        $data = array(
            'phone' => $phone,
            'nick' => $nick,
            'isadmin' => $isadmin,
            'status' => $status,
            'uname' => $uname,
            'description' => $description,
        );
        if ($verify) {
            $data['salt'] = DaoDbUserUser::SALT;
            $data['password'] = DaoDbUserUser::PASSWORD_LDAP;
        } else {
            if ($password) {
                $data['salt'] = uniqid();
                $data['password'] = sha1(sha1($password) . $data['salt']);
            }
        }

        return $this->saveUserInfo($data, $uid);
    }

    /**
     * 根据$uid取用户信息
     * @param $uid
     * @return array
     */
    public function getUserInfo($uid) {
        if ($uid) {
            $info = $this->getUserInfoById($uid);
            if ($info['password'] == DaoDbUserUser::PASSWORD_LDAP) {
                $info['verify'] = 1;
            } else {
                $info['verify'] = 0;
            }
        } else {
            $info = array();
        }

        return $info;
    }
}
